$(document).ready(function(){
	$("body").on("click",".form-header-info",function(){
		if($(".jdiag-header-info-container").length == 0 ){
			var form_json = $("body").data();
			var dialog_header_info
			=	'<div class="jdiag-header-info-container">'+
					'<div class="header-info-header">'+
						'<h3><i class="icon-save"></i> Header Information</h3>'+
						'<div class="hr"></div>'+
					'</div>'+
					'<div class="header-info-body">'+
						//'<label><input type="checkbox" class="enable-header-info"/> Enable header information</label>'+
						'<label><input type="checkbox" class="header-info-check-all"/> Check all</label>'+
						'<div class="hr"></div>'+
						'<div id="form-field-list-container" style="position: relative; margin: 0px auto; padding: 0px; width: 100%; overflow: hidden;height: 300px;">'+
                        	'<div class="content-dialog form-field-list-names" style="height: auto;">'+

                        	'</div>'+
                        '</div>'+
					'</div>'+
					'<div class="header-info-footer">'+
						'<div class="fields">'+
							'<div class="label_basic"></div>'+
							'<div class="input_position" style="margin-top:5px;text-align:right;">'+
								'<input type="button" class="btn-blueBtn save-header-info" value="Save">'+
								'<input type="button" class="btn-basicBtn cancel-header-info" value="Cancel">'+
							'</div>'+
						'</div>'+
					'</div>'+
				'</div>';

			jDialog(dialog_header_info, '', '', '', '', function(){
	        
	        });

	        //INITIALIZING PERFECT SCROLLBAR ... 
	        $("#form-field-list-container").perfectScrollbar();

	        //ADDING LIST OF HEADER INFO ON THE FORM
			var append_location_list_field = $("#popup_container").find("#popup_message").children(".jdiag-header-info-container").children(".header-info-body").children("#form-field-list-container").children(".form-field-list-names");
			$(".formbuilder_ws .setObject").each(function(eqindex){
				var self = $(this);
				if(//SKIP THE NON INPUT FIELDS
					self.attr("data-type") == "labelOnly" ||
					self.attr("data-type") == "createLine" ||
					self.attr("data-type") == "tab-panel" ||
					self.attr("data-type") == "table" ||
					self.attr("data-type") == "imageOnly" ||
					self.attr("data-type") == "listNames"
				){
					return true;
				}
				var data_obj_id = self.attr("data-object-id");
				var this_obj_label = self.find("#lbl_"+data_obj_id).text();
				var field_name = self.find(".getFields_"+data_obj_id).attr("name");
				var append_list_info = $(
					'<label header-info-object-id="'+data_obj_id+'">'+
						'<input type="checkbox" class="header-info-field" data-name="'+field_name+'" value="'+field_name+'"/> <span class="header-info-label">'+this_obj_label+'</span>'+
					'</label><br/>'
				);
				append_location_list_field.append(append_list_info);
			});

			//RESTORING SAVED VALUES
			if(typeof form_json.collected_data_header_info != "undefined"){
				if(typeof form_json.collected_data_header_info.listedchoice != "undefined"){ //THIS TYPEOF CONDITION ABLES US TO SECURE THE EXISTENCE OF THE SPECIFIC DATA BASED ON THE CONDITION
					for(var ctr = 0 ; ctr < form_json.collected_data_header_info.listedchoice.length ; ctr++){
						var saved_datas = form_json.collected_data_header_info.listedchoice[ctr];
						if( //I JUST DID THIS KIND OF CONDITION TO SECURE THE DATA INEGRITY OR SECURE THE ACCURATENESS OF RESTORING THE DATA OF A SAVED STATE
							// $(".header-info-field").eq(saved_datas.checkbox_index).length >= 1 &&
							// $(".header-info-field").eq(saved_datas.checkbox_index).closest("[header-info-object-id]").attr("header-info-object-id") == saved_datas.data_obj_id &&
							// $(".header-info-field").eq(saved_datas.checkbox_index).closest("[header-info-object-id]").children(".header-info-label").text() == saved_datas.field_label
							$("label[header-info-object-id='"+saved_datas.data_obj_id+"']").eq(0).length >= 1 &&
							$("label[header-info-object-id='"+saved_datas.data_obj_id+"']").eq(0).children(".header-info-label").text() == saved_datas.field_label
						){
							//$(".header-info-field").eq(saved_datas.checkbox_index).prop("checked",true); //RESTORED
							$("label[header-info-object-id='"+saved_datas.data_obj_id+"']").eq(0).children(".header-info-field[type='checkbox']").eq(0).prop("checked",true); //RESTORED
						}
					}
					var total_checkbox = $(".form-field-list-names").find(".header-info-field").length;
	        		var total_checked = $(".form-field-list-names").find(".header-info-field:checked").length;
	        		if(total_checkbox == total_checked){
	        			$(".header-info-check-all").prop("checked",true);
	        		}else{
	        			$(".header-info-check-all").prop("checked",false);
	        		}
				}
			}

	        //CHECK ALL CHECKBOX
	        $(".header-info-check-all").on({
	        	"click":function(){
	        		var is_checked = $(this).is(":checked");
	        		var self = $(this);
	        		if(is_checked){
	        			self.closest(".header-info-body").find(".form-field-list-names").find(".header-info-field").prop("checked",true);
	        		}else{
	        			self.closest(".header-info-body").find(".form-field-list-names").find(".header-info-field").prop("checked",false);
	        		}
	        	}
	        });

	        //CANCEL BUTTON
	        $(".cancel-header-info").on({
				"click":function(){
					$(this).closest("#popup_container").remove();
					$("#popup_overlay").remove();
				}
			});

			//SAVING BUTTON
			$(".save-header-info").on({
				"click":function(){
					var this_json = $("body").data();
					var self = $(this);
					collect_checked_header_info = {
						listedchoice:[]
						//,enable_header_info:$(".enable-header-info").is(":checked")
					};
					
					listed_choices_items = self.closest(".jdiag-header-info-container").find(".form-field-list-names").children("label[header-info-object-id]");
				
					listed_choices_items.each(function(eqindex){
						var self = $(this);
						if(self.children(".header-info-field[type='checkbox']").prop("checked") == true){
							collect_checked_header_info.listedchoice.push({
								//"checkbox_index":eqindex,
								"data_obj_id":self.attr("header-info-object-id"),
								"field_label":self.find(".header-info-label").text(),
								"field_name":self.find("[data-name]").val()
							});
						}
					});

					this_json.collected_data_header_info = collect_checked_header_info;
					console.log("ETTTOOO NA");
					console.log(this_json);
					$(this).closest("#popup_container").fadeOut(function(){
						$(this).remove();
					})
					$("#popup_overlay").fadeOut(function(){
						$(this).remove();
					})
				}
			});

			//CHECK INDIVIDUAL CHECKBOX
			$(".header-info-field").on({
	        	"click":function(){
	        		var is_checked = $(this).is(":checked");
	        		var total_checkbox = $(".form-field-list-names").find(".header-info-field").length;
	        		var total_checked = $(".form-field-list-names").find(".header-info-field:checked").length;
	        		if(total_checkbox == total_checked){
	        			$(".header-info-check-all").prop("checked",true);
	        		}else{
	        			$(".header-info-check-all").prop("checked",false);
	        		}
	        	}
	        })
		}
	});
});